<?php
/**
 * author      :chenwenwen
 * createTime  :2020-05-02 16:15
 * description :
 */

namespace app\api\controller;
class AuthBase extends ApiBase {
    public $accessToken = '';

    public $userId = 0;
    public $username = '';

    public function initialize() {
        parent::initialize();
        $this->accessToken = $this->request->header('access_token');
        if(!$this->accessToken || !$this->isLogin()) {
            return $this->show(config('status.error'),'请先登录');
        }
    }

    public function isLogin() {
        $token = $this->accessToken;
        $userInfo = cache(config('redis.token_pre') . $token);
        if(!$userInfo) return false;
        if($userInfo['id'] && $userInfo['username']) {
            $this->userId = $userInfo['id'];
            $this->username = $userInfo['username'];
            return true;
        }
    }
}